﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.ConstrainedExecution;
using System.Data;
using System.Drawing;

namespace _1042009_1042034
{
    class MyDataGridView:DataGridView
    {
        public int iPage = 1;
        public int iRecord = 6;
        public int iRecordDefaul = 7;
        public bool m_Reload;
        // cac ham he thong.( coi thoi khong nen sua)
        private List<string> ListColumnName = new List<string>();

        public int GetColumIndexByName(string ColumnName)
        {
            int index = new int();

            for (int i = 0; i < this.ListColumnName.Count; i ++)
            {
                if (this.ListColumnName[i] == ColumnName)
                {
                    index = i;
                }
            }
            return index;
        }

        private void AddColumnName(string ColumnName)
        {
            int Flag = 0;

            for (int i = 0; i < this.ListColumnName.Count; i++)
            {
                if (this.ListColumnName[i] == ColumnName)
                {
                    Flag ++;
                    break;
                }
            }
            if (Flag == 0) this.ListColumnName.Add(ColumnName);
        }
        public void AddRows()
        {
            try
            {
                this.Rows.Clear();
                if (this.MyDataTable.Rows.Count > iRecord)
                {
                    this.Rows.Add(this.iRecordDefaul);
                }
                else
                {
                    this.Rows.Add(this.MyDataTable.Rows.Count);
                }
            }
            catch
            { }

        }

        public void AddColumnStt()
        {
            DataGridViewTextBoxColumn TextColumn = new DataGridViewTextBoxColumn();
            TextColumn.HeaderText = "STT1";
            TextColumn.DataPropertyName = "STT1";
            TextColumn.ReadOnly = true;
            TextColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            TextColumn.Resizable = DataGridViewTriState.True;
            TextColumn.ValueType = Type.GetType("int");
            
            if (ReadOnly == true)
            {
                TextColumn.DefaultCellStyle.BackColor = MyColor.ReadOnly;
            }

            this.Columns.Add(TextColumn);
        }

        public void AddComboBoxColumns(bool ReadOnly, string ColumnName, string Header, object dataSource, string ValueMember, string DisplayMember)
        {
            this.AddColumnName(ColumnName);

            DataGridViewComboBoxColumn cbbColumn;
            cbbColumn = this.CreateComboBoxColumn(ColumnName, Header, ReadOnly);
            cbbColumn.ValueMember = ValueMember;
            cbbColumn.DisplayMember = DisplayMember;
            cbbColumn.DataSource = dataSource;
            

            this.Columns.Add(cbbColumn);             
        }
        public void AddLinkColumn(string ColumnName, string Header)
        {
            this.AddColumnName(ColumnName);
            DataGridViewLinkColumn links = new DataGridViewLinkColumn();

            links.HeaderText = Header;
            links.DataPropertyName = ColumnName;
            links.ActiveLinkColor = Color.White;
            links.LinkBehavior = LinkBehavior.SystemDefault;
            links.LinkColor = Color.Blue;
            links.TrackVisitedState = true;
            links.VisitedLinkColor = Color.YellowGreen;

            this.Columns.Add(links);
        }
        private DataGridViewComboBoxColumn CreateComboBoxColumn(string ColumnName, string Header, bool ReadOnly)
        {
            this.AddColumnName(ColumnName);
            DataGridViewComboBoxColumn cbbColumn = new DataGridViewComboBoxColumn();
            cbbColumn.Name = ColumnName;
            cbbColumn.HeaderText = Header;
            cbbColumn.DropDownWidth = 160;
            cbbColumn.Width = 90;
            
            //cbbColumn.DisplayStyleForCurrentCellOnly = true;
            cbbColumn.DisplayStyleForCurrentCellOnly = true;

            cbbColumn.MaxDropDownItems = 3;
          
            if (ReadOnly == true)
            {
                cbbColumn.ReadOnly = ReadOnly;
                cbbColumn.DefaultCellStyle.BackColor = MyColor.ReadOnly;
                cbbColumn.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
            }
            cbbColumn.FlatStyle = FlatStyle.Flat;
            return cbbColumn;

        }
        public void AddTextColumn(bool ReadOnly, string ColumnName, string Header)
        {
            this.AddColumnName(ColumnName);
            DataGridViewTextBoxColumn TextColumn = new DataGridViewTextBoxColumn();
            TextColumn.HeaderText = Header;
            TextColumn.DataPropertyName = ColumnName;
            TextColumn.ReadOnly = ReadOnly;
            TextColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            if (ReadOnly == true)
            {
                TextColumn.DefaultCellStyle.BackColor = MyColor.ReadOnly;
            }

            this.Columns.Add(TextColumn);
        }
        public void AddButtonColumn(string ColumnName, string Header)
        {
            this.AddColumnName(ColumnName);
            DataGridViewButtonColumn buttons = new DataGridViewButtonColumn();
            buttons.HeaderText = ColumnName;
            buttons.Text = Header;
            buttons.UseColumnTextForButtonValue = true;
            buttons.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            buttons.FlatStyle = FlatStyle.Standard;
            buttons.CellTemplate.Style.BackColor = Color.Honeydew;
            buttons.DisplayIndex = 0;
           
            this.Columns.Add(buttons);
        }
        public void AddDateTimeColumn(bool ReadOnly, string ColumnName, string Header)
        {
            this.AddColumnName(ColumnName);
            DataGridViewTextBoxColumn TextColumn = new DataGridViewTextBoxColumn();
            TextColumn.HeaderText = Header;
            TextColumn.DataPropertyName = ColumnName;
            TextColumn.ReadOnly = ReadOnly;            
            this.Columns.Add(TextColumn);
        }
        public void AddCheckBoxColumn(string ColumnName, string Header)
        {
            this.AddColumnName(ColumnName);
            DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
            column.HeaderText = Header;
            column.Name = ColumnName;
            column.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            column.FlatStyle = FlatStyle.Standard;
            column.CellTemplate.Style.BackColor = Color.Beige;
            column.TrueValue = "Checked";
            column.FalseValue = "Unchecked";
            this.Columns.Add(column);
        }
        public void uctMyDataGridView_Load(object sender, EventArgs e)
        {
        }
        public void RemoveRows(int iRow)
        {
            try
            {
                this.MyDataTable.Rows.RemoveAt(iRow);
            }
            catch { }
        }
        public DataTable MyDataTable; 

        public void SetRowColor( int iRow, Color color, bool AllCells = false)
        {
            for (int i = 0; i < this.Columns.Count; i++)
            {
                if (AllCells == false)
                {
                    if (this.Columns[i].ReadOnly == false)
                    {
                        this.Rows[iRow].Cells[i].Style.BackColor = color;
                    }
                    else
                    { 
                        this.Rows[iRow].Cells[i].Style.BackColor = MyColor.ReadOnly;
                    }
                }
                else
                { 
                    this.Rows[iRow].Cells[i].Style.BackColor = color;
                }
            }
        }
    }
}
